package cn.wolfcode.shop.cloud.mq;

import cn.wolfcode.shop.cloud.service.IOrderInfoService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RocketMQMessageListener(
        consumerGroup = MQConstans.CREATE_ORDER_DELAY_GROUP,
        topic = MQConstans.SECKILL_ORDER_TOPIC,
        selectorExpression = MQConstans.CREATE_ORDER_DELAY_TAG
)
public class OrderSuccessDelayMQListener implements RocketMQListener<Map> {

    @Autowired
    private IOrderInfoService orderInfoService;
    @Override
    public void onMessage(Map message) {
        System.out.println("进入到延迟队列了");
        //消息是map类型的时候存在这么一个情况
        //如果map中的key value 中有int long
        //内部会进行一个转化,如果长度比较小的认为你是一个int类型
        //如果你的长度比较大它认为是个long类型
        //订单号
        String orderNo = (String) message.get("data");
        //秒杀商品id
        Object obj = message.get("seckillId");

        Long goodId = null;
        if(obj instanceof Integer){
            goodId = ((Integer) obj).longValue() ;
        }else {
            goodId = (Long) obj;
        }

        //取消订单
        orderInfoService.cancelOrder(orderNo,goodId);
    }
}
